package com.jic.order.controller.rest;

import com.jic.common.base.vo.RestResult;
import com.jic.order.rest.request.*;
import com.jic.order.rest.response.ShoppingCartAddResponse;
import com.jic.order.rest.response.ShoppingCartInfoListQueryResponse;
import com.jic.order.api.ShoppingCartFeign;
import com.jic.order.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: gaolanyu
 * @date: 2020-01-23
 * @remark:
 */
@RestController
@RequestMapping("/shopping-cart")
@Api(tags = {"购物车H5页面API"})
@Slf4j
public class ShoppingCartController implements ShoppingCartFeign {


    @Autowired
    ShoppingCartService shoppingCartService;

    @ApiOperation(value = "查询购物车接口")
    @Override
    public RestResult<ShoppingCartInfoListQueryResponse> queryShoppingCart(ShoppingCartInfoListQueryRequest shoppingCartInfoListQueryRequest) {
        log.info("ShoppingCartController==>queryShoppingCart==>,req,token:{}", shoppingCartInfoListQueryRequest.getToken());
        String userId = shoppingCartService.getUserIdByToken(shoppingCartInfoListQueryRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartInfoListQueryRequest.setUserId(Long.parseLong(userId));
        }
        log.info("ShoppingCartController==>queryShoppingCart==>,req,userId:{}", shoppingCartInfoListQueryRequest.getUserId());
        RestResult<ShoppingCartInfoListQueryResponse> restResult = shoppingCartService.queryShoppingCart(shoppingCartInfoListQueryRequest, false);
        log.info("ShoppingCartController==>queryShoppingCart==>,resp:{}，data:{}", restResult, restResult.getData());
        return restResult;
    }

    @ApiOperation(value = "增加商品到购物车")
    @Override
    public RestResult<ShoppingCartAddResponse> add(ShoppingCartAddRequest shoppingCartAddRequest) {
        String userId = shoppingCartService.getUserIdByToken(shoppingCartAddRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartAddRequest.setUserId(Long.parseLong(userId));
        }

        log.info("ShoppingCartController==>add==>,userId:{},req:{}", shoppingCartAddRequest.getUserId(), shoppingCartAddRequest);
        RestResult<ShoppingCartAddResponse> restResult = shoppingCartService.add(shoppingCartAddRequest, false);
        log.info("ShoppingCartController==>add==>,userId:{},restResult:{},data:{}", userId, restResult, restResult.getData());
        return restResult;
    }

    @ApiOperation(value = "批量删除购物车里的商品")
    @Override
    public RestResult<Boolean> delete(ShoppingCartDeleteRequest shoppingCartDeleteRequest) {
        String userId = shoppingCartService.getUserIdByToken(shoppingCartDeleteRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartDeleteRequest.setUserId(Long.parseLong(userId));
        }
        log.info("ShoppingCartController==>delete==>,userId:{},req:{}", shoppingCartDeleteRequest.getUserId(), shoppingCartDeleteRequest);
        RestResult<Boolean> restResult = shoppingCartService.delete(shoppingCartDeleteRequest, false);
        log.info("ShoppingCartController==>delete==>,userId:{},resp:{}", shoppingCartDeleteRequest.getUserId(), restResult);
        return restResult;
    }

    @ApiOperation(value = "编辑购物车商品数量")
    @Override
    public RestResult<Boolean> editNum(ShoppingCartEditRequest shoppingCartEditRequest) {
        String userId = shoppingCartService.getUserIdByToken(shoppingCartEditRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartEditRequest.setUserId(Long.parseLong(userId));
        }
        log.info("ShoppingCartController==>editNum==>,userId:{},req:{}", shoppingCartEditRequest.getUserId(), shoppingCartEditRequest);
        RestResult<Boolean> restResult = shoppingCartService.editNum(shoppingCartEditRequest, false);
        log.info("ShoppingCartController==>editNum==>,userId:{},resp:{}", shoppingCartEditRequest.getUserId(), restResult);
        return restResult;
    }

    @ApiOperation(value = "选中购物车里的商品进行结算")
    @Override
    public RestResult<Boolean> selected(ShoppingCartSelectedRequest shoppingCartSelectedRequest) {
        String userId = shoppingCartService.getUserIdByToken(shoppingCartSelectedRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartSelectedRequest.setUserId(Long.parseLong(userId));
        }
        log.info("ShoppingCartController==>selected==>,userId:{},req:{}", shoppingCartSelectedRequest.getUserId(), shoppingCartSelectedRequest);
        RestResult<Boolean> restResult = shoppingCartService.selected(shoppingCartSelectedRequest, false);
        log.info("ShoppingCartController==>selected==>,userId:{},resp:{}", shoppingCartSelectedRequest.getUserId(), restResult);
        return restResult;
    }

    @ApiOperation(value = "取消选中购物车里的商品")
    @Override
    public RestResult<Boolean> unSelected(ShoppingCartSelectedRequest shoppingCartSelectedRequest) {
        String userId = shoppingCartService.getUserIdByToken(shoppingCartSelectedRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            shoppingCartSelectedRequest.setUserId(Long.parseLong(userId));
        }
        log.info("ShoppingCartController==>unSelected==>,userId:{},req:{},directFlag:{}", shoppingCartSelectedRequest.getUserId(), shoppingCartSelectedRequest, false);
        RestResult<Boolean> restResult = shoppingCartService.unSelected(shoppingCartSelectedRequest, false);
        log.info("ShoppingCartController==>unSelected==>,userId:{},resp:{},directFlag:{}", shoppingCartSelectedRequest.getUserId(), restResult, false);
        return restResult;
    }

    @Override
    @ApiOperation(value = "查询购物车商品总数")
    public RestResult<Integer> queryShoppingCartNum(UserRequest userRequest) {
        String userId = shoppingCartService.getUserIdByToken(userRequest.getToken());
        if (!StringUtils.isEmpty(userId)) {
            userRequest.setUserId(Long.parseLong(userId));
        }
        RestResult<Integer> restResult = shoppingCartService.queryShoppingCartNum(userRequest, false);
        return restResult;
    }
}
